# -*- coding: utf-8 -*-
from tornado.web import UIModule

from bopress.hook import Hooks, add_action
from bopress.utils import Utils

__author__ = 'yezang'


class Screen(object):
    def __init__(self, screen_id="", handler=None):
        self.id = screen_id
        self.handler = handler
        self.current_menu = None


class AbstractDataProvider(object):
    """
        为 `BoListTable` 提供数据源
    """

    def __init__(self):
        pass

    def column_titles(self, handler):
        """
        column titles, return list
        example::
            return ['Title 1', 'Title 2', ..]
        :param handler: `.handlers.BaseHandler`
        """
        raise NotImplementedError()

    def columns(self, handler):
        """
        field name from api result::
            return ['post_id', 'title', 'content', ..]
        :param handler: `.handlers.BaseHandler`
        """
        raise NotImplementedError()

    def column_render(self):
        """
        return column render js function name.
        :return: str
        """
        return ""

    def ajax_params(self):
        """
        ajax request extra params js callback function name.
        :return: str
        """
        return ""

    def data(self, handler, params):
        """
        api data
        :param dict params: jquery datatables 传递过来的查询参数.
        :param handler: `.handlers.BaseHandler`

            example::
                s = SessionFactory.session()
                return s.query(Posts).all()

        """
        raise NotImplementedError()

    def searchable_columns(self, handler):
        """
        searchable columns
        :param handler: `.handlers.BaseHandler`
        :return: list

            example::
                return ['title', 'content']

        """
        return []

    def orderable_columns(self, handler):
        """
        orderable columns
        :param handler: `.handlers.BaseHandler`
        :return: list

            example::
                return ['title']

        """
        return []

    def hide_columns(self, handler):
        """
        hide columns
        :param handler: `.handlers.BaseHandler`
        :return: list

            example::
                return ['post_id']

        """
        return []

    def render_json(self, handler, dataset=None, records_total=0, records_filtered=0, params=None):
        """
        为JQuery DataTables 返回JSON数据
        :param params: JQuery DataTables 查询参数.
        :param dataset: 表数据
        :param records_total: 总记录数
        :param records_filtered: 总过滤记录数
        """
        if not dataset:
            dataset = list()
        r = dict()
        r["draw"] = params.get("draw", 1)
        r["recordsTotal"] = records_total
        r["recordsFiltered"] = records_filtered
        r["data"] = dataset
        handler.head_json()
        handler.write(Utils.encode(r))


class BoListTable(UIModule):
    def render(self, table_id="", data_provider_cls=None):
        if not table_id and not data_provider_cls:
            return ""
        self.api_name = Utils.md5(table_id)
        tables = Hooks.ui_list_tables()
        provider = tables.get(self.api_name, None)
        if not provider:
            provider = data_provider_cls()
            tables[self.api_name] = provider
        self.table_id = table_id
        return self.render_string("bocore/admin/ui/listtable_body.html",
                                  table_id=table_id,
                                  titles=provider.column_titles(self.handler))

    def html_body(self):
        return self.render_string("bocore/admin/ui/listtable_script.html",
                                  table_id=self.table_id,
                                  provider=Hooks.ui_list_tables()[self.api_name],
                                  api_name=self.api_name
                                  )

    def javascript_files(self):
        return [
            "theme/plugins/datatables/jquery.dataTables.min.js",
            "theme/plugins/datatables/dataTables.bootstrap.min.js",
            "theme/plugins/select2/select2.full.min.js"
        ]

    def css_files(self):
        return [
            "theme/plugins/datatables/datatables.bootstrap.css",
            "theme/plugins/select2/select2.min.css"
        ]
